Download
FAQ
History
HomeHomeNext API
Search
Feedback
Divider

A J2EE Application that Uses the JMS API with a Session Bean

This section explains how to write, compile, package, deploy, and run a J2EE application that uses the JMS API in conjunction with a session bean. The application contains the following components:

The section covers the following topics:

You will find the source files for this section in the directory <INSTALL>/j2eetutorial14/examples/jms/clientsessionmdb/. Path names in this section are relative to this directory.

Writing the Application Components

This application demonstrates how to send messages from an enterprise bean--in this case, a session bean--rather than from an application client, as in the example in A Message-Driven Bean Example. Figure 21-2 illustrates the structure of this application.

A J2EE Application: Client to Session Bean to Message-Driven Bean

Figure 21-2 A J2EE Application: Client to Session Bean to Message-Driven Bean

The Publisher enterprise bean in this example is the enterprise-application equivalent of a wire-service news feed that categorizes news events into six news categories. The message-driven bean could represent a newsroom, where the Sports desk, for example, would set up a subscription for all news events pertaining to sports.

The application client in the example obtains a handle to the Publisher enterprise bean's remote home interface and calls the enterprise bean's business method. The enterprise bean creates 18 text messages. For each message, it sets a String property randomly to one of six values representing the news categories and then publishes the message to a topic. The message-driven bean uses a message selector for the property to limit which of the published messages it receives.

Writing the components of the application involves

Coding the Application Client: MyAppClient.java

The application client program, src/MyAppClient.java, performs no JMS API operations and so is simpler than the client program in A Simple J2EE Application that Uses the JMS API. The program obtains a handle to the Publisher enterprise bean's remote home interface, using the JNDI naming context java:comp/env. The program then creates an instance of the bean and calls the bean's business method twice.

Coding the Publisher Session Bean

The Publisher bean is a stateless session bean with one create method and one business method. The Publisher bean uses remote interfaces rather than local interfaces because it is accessed from outside the EJB container.

The remote home interface source file is src/PublisherHome.java.

The remote interface, src/PublisherRemote.java, declares a single business method, publishNews.

The bean class, src/PublisherBean.java, implements the publishNews method and its helper method chooseType. The bean class also implements the required methods ejbCreate, setSessionContext, ejbRemove, ejbActivate, and ejbPassivate.

The ejbCreate method of the bean class allocates resources--in this case, by looking up the ConnectionFactory and the topic and creating the Connection. The business method publishNews creates a Session and a MessageProducer and publishes the messages.

The ejbRemove method must deallocate the resources that were allocated by the ejbCreate method. In this case, the ejbRemove method closes the Connection.

Coding the Message-Driven Bean: MessageBean.java

The message-driven bean class, src/MessageBean.java, is identical to the one in A Simple J2EE Application that Uses the JMS API. However, the deployment descriptor will be different, because the bean is using a topic with a durable subscription, instead of a queue.

Creating and Packaging the Application

This example uses the topic named jms/Topic and the connection factory jms/TopicConnectionFactory, which you created in Creating the JMS Administered Objects for the Simple Examples.

Creating and packaging this application involve several steps:

Compiling the Source Files and Starting the J2EE Application Server

  1. Use the build target to compile the source files:
  2. asant build

  3. Start the J2EE Application Server, if it is not already running.

Starting the J2EE Deploytool and Creating the Application

  1. Start the deploytool.
  2. Choose FileRight ArrowNewRight ArrowApplication EAR.
  3. Click Browse next to the Application File Name field and use the file chooser to locate the directory clientsessionmdb.
  4. In the File Name field, type ClientSessionMDBApp.
  5. Click New Application.
  6. Click OK.

Packaging the Application Client

To package the application client, perform the following steps:

  1. Choose FileRight ArrowNewRight ArrowApplication Client JAR to start the Application Client Wizard.
  2. In the JAR File Contents screen:
    1. Verify that Create New AppClient Module in Application is selected and that the application is ClientSessionMDBApp.
    2. In the AppClient Display Name field, type MyAppClient.
    3. Click the Edit button next to the Contents text area.
    4. In the dialog box, locate the build/client/ directory. Select MyAppClient.class from the Available Files tree area and click Add, then OK.
  3. In the General screen, choose client.MyAppClient in the Main Class combo box.
  4. In the Enterprise Bean References screen, click Add. In the dialog box:
    1. Type ejb/remote/Publisher in the Coded Name field.
    2. Choose Session from the EJB Type menu.
    3. Choose Remote from the Interfaces menu.
    4. Type sb.PublisherHome in the Home Interface field.
    5. Type sb.PublisherRemote in the Local/Remote Interface field.
    6. Leave the Enterprise Bean Name field blank.
    7. The client accesses the bean from outside of the EJB container, so the session bean uses remote interfaces that are accessed through the JNDI name.

  5. When you return to the Enterprise Bean References screen, select the bean. In the Deployment Settings area, select JNDI Name and type Publisher in the field.
  6. Click Finish.

Packaging the Session Bean

To package the session bean, perform the following steps:

  1. Choose FileRight ArrowNewRight ArrowEnterprise JavaBean JAR to start the Enterprise Bean Wizard, then click Next.
  2. In the EJB JAR screen:
    1. Select Create New JAR Module in Application and verify that the application is ClientSessionMDBApp. In the JAR Display Name field, type EBJAR.
    2. Click the Edit button next to the Contents text area.
    3. In the dialog box, locate the build/sb/ directory. Select PublisherBean.class, PublisherHome.class, and PublisherRemote.class from the Available Files tree area and click Add, then OK.
  3. In the General screen:
    1. Choose the Session radio button.
    2. Choose the Stateless radio button.
    3. From the Enterprise Bean Class menu, choose sb.PublisherBean.
    4. In the Enterprise Bean Display Name field, type Publisher.
    5. In the Remote Interfaces area, choose sb.PublisherHome from the Remote Home Interface menu and sb.PublisherRemote from the Remote Interface menu.
  4. In the Configuration Options screen, check the Message Destination References, Resource References, and Transaction Management boxes.
  5. In the Transaction Management screen, choose Container-Managed.
  6. In the Message Destination References screen, click Add. In the dialog box:
    1. Type jms/TopicName in the Coded Name field.
    2. Choose javax.jms.Topic from the Destination Type menu.
    3. Choose Produces from the Usage menu.
    4. Type PhysicalTopic in the Destination Name field.
  7. In the Resource References screen:
    1. Click Add.
    2. Type jms/MyConnectionFactory in the Coded Name field.
    3. Choose javax.jms.ConnectionFactory from the Type menu.
    4. Type jms/TopicConnectionFactory in the JNDI name field and j2ee in both the User Name and Password fields.
  8. Click Finish.
  9. Click the EBJAR node, then click the Message Destinations tab. In the inspector pane:
    1. Click Add.
    2. Type PhysicalTopic in the Destination Name field. When you press Enter, this name appears in the Display Name field, and Publisher appears in the Producers area.
    3. Type jms/Topic in the JNDI Name field.

Packaging the Message-Driven Bean

You will package the message-driven bean in the same JAR file as the session bean, for greater efficiency.

To package the message-driven bean, perform the following steps:

  1. Choose FileRight ArrowNewRight ArrowEnterprise JavaBean JAR to start the Enterprise Bean Wizard, then click Next.
  2. In the EJB JAR screen:
    1. Choose Add to Existing JAR Module and verify that the module is EBJAR (ClientSessionMDBApp).
    2. Click the Edit button next to the Contents text area.
    3. In the dialog box, locate the build/mdb/ directory. Select MessageBean.class from the Available Files tree area and click Add, then OK.
  3. In the General screen:
    1. Choose the Message-Driven radio button.
    2. From the Enterprise Bean Class menu, choose mdb.MessageBean.
    3. In the Enterprise Bean Display Name field, accept the default value, MessageBean.
  4. In the Configuration Options screen, check the Transaction Management box.
  5. In the Message-Driven Bean Settings screen:
    1. Click Add in the Activation Configuration Properties area six times to type the property names and values shown in Table 21-3.
    2. Choose javax.jms.Topic from the Destination Type menu.
    3. Choose PhysicalTopic from the Destination Name menu.
    4. Click Deployment Settings. In the dialog box, type jmsra in the Resource Adapter field and click OK.
  6. In the Transaction Management screen, choose Container-Managed.
  7. Click Finish.
  8. Click the Message Destinations tab. You will see that MessageBean now appears in the Consumers area.

The activation configuration property values for the bean are shown in Table 21-3.

Table 21-3 Activation Configuration Properties for a Durable Subscriber
Property Name
Value
destinationType
javax.jms.Topic
destination
PhysicalTopic
messageSelector
NewsType = 'Sports' OR NewsType = 'Opinion'
subscriptionDurability
Durable
clientId
MyID
subscriptionName
MySub

Updating the JNDI Names

You need to update the JNDI name for the message-driven bean so that it specifies the destination it receives messages from, not the bean name.

  1. Select the application and click the JNDI Names tab.
  2. Type jms/Topic in the JNDI Name field for the MessageBean component.

Verify that the JNDI names for the application components are correct. They should appear as shown in Table 21-4 and Table 21-5.

Table 21-4 Application Pane for ClientSessionMDBApp
Component Type
Component
JNDI Name
EJB
MessageBean
jms/Topic
EJB
Publisher
Publisher

Table 21-5 References Pane for ClientSessionMDBApp
Ref. Type
Referenced By
Reference Name
JNDI Name
EJB Ref
MyAppClient
ejb/remote/Publisher
Publisher
Resource
Publisher
jms/MyConnectionFactory
jms/TopicConnectionFactory

Deploying the Application

  1. Choose FileRight ArrowSave to save the application.
  2. Choose ToolsRight ArrowDeploy.
  3. In the dialog box, type your administrative user name and password (if they are not already filled in) and click OK.
  4. In the Distribute Module dialog box, click Close when the process completes.

Saving the Client JAR and Running the Application

  1. In the deploytool, select the server node.
  2. Select ClientSessionMDBApp from the list of deployed objects, then click Client Jar.
  3. Choose Browse to navigate to the directory from which you will run the client. Specify the clientsessionmdb directory, click Select, then click OK. Click OK in the information dialog. You will find a file named ClientSessionMDBAppClient.jar in the specified directory.
  4. To run the client, use the following command:
  5. appclient -client ClientSessionMDBAppClient.jar

The program output in the terminal window looks something like this:

Looking up EJB reference
Looked up home
Narrowed home
Got the EJB 

The output from the enterprise beans appears in the server log. The Publisher session bean sends two sets of 18 messages numbered 0 through 17. Because of the message selector, the message-driven bean receives only the messages whose NewsType property is Sports or Opinion.

Suppose that the last few messages from the Publisher session bean look like this:

PUBLISHER: Setting message text to: Item 12: Business
PUBLISHER: Setting message text to: Item 13: Opinion
PUBLISHER: Setting message text to: Item 14: Living/Arts
PUBLISHER: Setting message text to: Item 15: Sports
PUBLISHER: Setting message text to: Item 16: Living/Arts
PUBLISHER: Setting message text to: Item 17: Living/Arts 

Because of the message selector, the last messages received by the message-driven bean will be the following:

MESSAGE BEAN: Message received: Item 13: Opinion
MESSAGE BEAN: Message received: Item 15: Sports 

If you like, you can rewrite the message selector to receive different messages.

Undeploying and Removing the Application

To undeploy and remove the application, perform the following steps:

  1. Select the deployed application in the server pane and click Undeploy.
  2. Select the application in the left-hand pane and choose FileRight ArrowClose.
  3. Remove the build directory created by the build target:
  4. asant clean

  5. If you wish, stop the server:
  6. asadmin stop-domain

If you wish, you can manually delete the EAR and client JAR files.

Divider
Download
FAQ
History
HomeHomeNext API
Search
Feedback
Divider

All of the material in The J2EE 1.4 Tutorial is copyright-protected and may not be published in other works without express written permission from Sun Microsystems.